package com.comiee.tankwar;

import java.util.ArrayList;
import java.util.List;

import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.maps.MapLayer;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
/**
 * 地图信息类
 * @author comiee
 *
 */
public class Terrain {
	private List<String[]> maps = new ArrayList<String[]>();	//存储地图信息
	private Texture m1 = new Texture("images/stone.png");	//石头方块
	private Texture m2 = new Texture("images/wall.png");	//墙方块
	private Texture m3 = new Texture("images/grass.png"); //草方块
	
	private char[][] data;	//存储当前位置方块信息
	private int size = 64;	//一个方块的边长为64像素
	private int width=30,height=30;	//地图大小（单位方块）
	List<Sprite> list1;	//不可破坏方块
	List<Sprite> list2;	//可破坏方块
	List<Sprite> list3;	//遮挡方块
	
	public Terrain(int index) {
		data = new char[width][height];
		list1 = new ArrayList<Sprite>();
		list2 = new ArrayList<Sprite>();
		list3 = new ArrayList<Sprite>();
		for(int i=0;i<height;i++) {
			for(int j=0;j<width;j++) {
				data[j][i]='0';	//初始化
			}
		}/*
		String[] m = maps.get(index);
		for(int i=0;i<height;i++) {
			String a = m[height-1-i];
			for(int j=0;j<width;j++) {
				char b = a.charAt(j); 
				data[j][i]=b;	//读入地图信息
			}
		}*/
		TiledMap map = new TmxMapLoader().load("map/map"+(index+1)+".tmx");
		for (MapLayer layer : map.getLayers()) {
			TiledMapTileLayer tl = (TiledMapTileLayer) layer;
			for(int i=0;i<height;i++) {
				for(int j=0;j<width;j++) {
					if(tl.getCell(j, i)!=null) {
						data[j][i]=layer.getName().charAt(0);
					}
				}
			}
		}
	}
	
	public void add(int x,int y,char b) {
		Sprite sprite;
		data[x][y]=b;
		switch(b) {
		case '0':
			break;
		case '1':
			sprite = new Sprite(m1);
			sprite.setPosition(x*size, y*size);
			list1.add(sprite);
			break;
		case '2':
			sprite = new Sprite(m2);
			sprite.setPosition(x*size, y*size);
			list2.add(sprite);
			break;
		case '3':
			sprite = new Sprite(m3);
			sprite.setPosition(x*size, y*size);
			list3.add(sprite);
			break;
		}
	}
	public void delete(int x,int y) {
		data[x][y] = '0';
		for(Sprite i : list1) {
			if(i.getX()==x && i.getY()==y) {
				list1.remove(i);
				break;
			}
		}
	}
	
	public void update() {
		list1.clear();	//清空列表
		list2.clear();
		list3.clear();
		for(int i=0;i<height;i++) {
			for(int j=0;j<width;j++) {
				add(j,i,data[j][i]);
			}
		}
	}
	
	public void draw(SpriteBatch batch,int... arg) {
		for(int j : arg) {
			List<Sprite> list= new ArrayList<Sprite>();
			switch(j) {
			case 1: list=list1; break;
			case 2: list=list2; break;
			case 3: list=list3; break;
			}
			for(Sprite i : list) {
				i.draw(batch);
			}
		}
	}
	
	public void dispose() {
		this.m1.dispose();
		this.m2.dispose();
		this.m3.dispose();
	}
	
	{	//利用构造代码块写入地图信息
		maps.add(new String[]{	"111111111111111111111111111111",
								"100000000000000000000000000001",
								"100000000000000000000000000001",
								"100000000000000000000000000001",
								"100010000222200002222000010001",
								"100010000222200002222000010001",
								"100010000000000000000000010001",
								"100010000000000000000000010001",
								"100010000000000000000000010001",
								"100010000222200002222000010001",
								"100010000222200002222000010001",
								"100010000000000000000000010001",
								"100010000000000000000000010001",
								"100000000000000000000000000001",
								"100000003333333333333300000001",
								"100000003333333333333300000001",
								"100000000000000000000000000001",
								"100010000000000000000000010001",
								"100010000000000000000000010001",
								"100010000222200002222000010001",
								"100010000222200002222000010001",
								"100010000000000000000000010001",
								"100010000000000000000000010001",
								"100010000000000000000000010001",
								"100010000222200002222000010001",
								"100010000222200002222000010001",
								"100000000000000000000000000001",
								"100000000000000000000000000001",
								"100000000000000000000000000001",
								"111111111111111111111111111111",
		});
	}
}
